//! \file UiInput.c
//! \brief TODO: Document
// -----------------------------------------------------------------------------
//! This program is free software; you can redistribute it and/or
//! modify it under the terms of the GNU General Public License
//! as published by the Free Software Foundation; either version 2
//! of the License, or (at your option) any later version.
//!
//! This program is distributed in the hope that it will be useful,
//! but WITHOUT ANY WARRANTY; without even the implied warranty of
//! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//! GNU General Public License for more details.
//!
//! You should have received a copy of the GNU General Public License
//! along with this program; if not, write to the Free Software Foundation,
//! Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// -----------------------------------------------------------------------------

#include "UiInput.h"
#include <SDL/SDL.h>
#include "DataBase.h"

// =====[ Local Definitions ]===================================================

// =====[ Local DataTypes ]=====================================================

// =====[ Local Variables ]=====================================================

// =====[ Local Functions ]=====================================================
static void _UI_InputCheckKeys(SDL_Event* pEvent);

// =====[ Local Implementation ]================================================


// =============================================================================
// Implement: UI_InputCheck
// -----------------------------------------------------------------------------
void UI_InputCheck(void)
{
  SDL_Event Event;

  // Poll for input events and check if one exists or not.
  if (SDL_PollEvent(&Event) == 0) return;

  // Process the event according to the type of even we received.
  switch (Event.type)
  {
    case SDL_KEYDOWN:
      _UI_InputCheckKeys(&Event);
      break;
    case SDL_ACTIVEEVENT:
    case SDL_KEYUP:
    case SDL_MOUSEMOTION:
    case SDL_MOUSEBUTTONDOWN:
    case SDL_MOUSEBUTTONUP:
    case SDL_JOYAXISMOTION:
    case SDL_JOYBALLMOTION:
    case SDL_JOYHATMOTION:
    case SDL_JOYBUTTONDOWN:
    case SDL_JOYBUTTONUP:
    case SDL_QUIT:
    case SDL_SYSWMEVENT:
    case SDL_VIDEORESIZE:
    case SDL_VIDEOEXPOSE:
    default:
      // Do nothing.
      break;
  }
}


// =============================================================================
// _UI_InputCheckKeys
// -----------------------------------------------------------------------------
//! TODO: document
//!
//! \param [in] Arg
//!   TODO: document
//!
//! \retval Exception
//!   The success status of the requested operation.
//!   - eOKAY: Execution occurred as requested.
//!   .
//!
//! \b Example: \code
//! FxName(0);
//! \endcode
// -----------------------------------------------------------------------------
static void _UI_InputCheckKeys(SDL_Event* pEvent)
{
  static SDLKey Last;

  // Validate our arguments.
  if (pEvent == NULL) return;

  // Verify the input event is a key press.
  if (pEvent->type != SDL_KEYDOWN) return;

  // Process the input as needed.
  Last = pEvent->key.keysym.sym;
  switch (pEvent->key.keysym.sym)
  {
    case SDLK_q:
      // Set that the application is no longer running.
      DB_SetEnum(eKEY_APPLICATION_IS_RUNNING, 0, eFALSE);
      break;
    default:
      // Do nothing.
      break;

//    case SDLK_UNKNOWN:
//      break;

/*

      //If there's an event to handle
      if( SDL_PollEvent( &event ) )
      {
        //If a key was pressed
        if( event.type == SDL_KEYDOWN )
        {
          //Set the proper message surface
          switch( event.key.keysym.sym )
          {

          */

/*    case SDL_KEYDOWN:
      switch (*SDL_GetKeyName(Event.key.keysym.sym))
      {
        case 'Q':
        case 'q':
          //UI_Shutdown();
          //Tasks_Kill();
          //_Main_Shutdown();
          // Set us as running.
          DB_SetRunning(eFALSE);
          return (0);
        case 'R':
        case 'r':
          //Value = Dice_Roll(1, 6, 2);

          //Value = Dice_Roll(dDICE_1d6_1);
//            dDICE_1d6_1
          //tDICE Dice = {1, 6, 2};
          //Value = Dice_Roll2(Dice);
          break;
      }
      printf("The %s key was pressed!\n", SDL_GetKeyName(Event.key.keysym.sym));
      break;
    case SDL_QUIT:
      exit(0);*/
  }

}



/* Tie a global hotkey to a callback function. */
//void
//AG_BindGlobalKey(AG_KeySym keysym, AG_KeyMod keymod, void (*fn)(void))
//{
//        struct ag_global_key *gk;
//
//        gk = Malloc(sizeof(struct ag_global_key));
//        gk->keysym = keysym;
//        gk->keymod = keymod;
//        gk->fn = fn;
//        gk->fn_ev = NULL;
//
//        AG_MutexLock(&agGlobalKeysLock);
//        SLIST_INSERT_HEAD(&agGlobalKeys, gk, gkeys);
//        AG_MutexUnlock(&agGlobalKeysLock);
//}
